<?php
error_reporting(0);
session_start();

if (!isset($_SESSION["email"])) {
    include "includes/accessDeniedInclude.php";
} else {
    include_once "src/controller/blmanagement/UserAccountManagement.php";
    include_once "src/model/User.php";

    $isAdmin = false;
    $user = UserAccountManagement::getUserByEmail($_SESSION["email"]);
    $userId = $user->getUserId();
    $userType = $user->getType();
    if ($user->getType() == "1") {
        $isAdmin = true;
    }
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Resource Reservation System</title>
        <link href="js/jquery/jquery-ui-1.7.2.custom/css/smoothness/jquery-ui-1.7.2.custom.css"
              rel="stylesheet"
              type="text/css"
              media="all">
        <link href="js/jquery/fullcalendar-1.4.5/fullcalendar.css"
              rel="stylesheet"
              type="text/css"
              media="all">
        <link href="styles/default.css"
              rel="stylesheet"
              type="text/css"
              media="all">
        <script type="text/javascript"
        src="js/jquery/jquery-1.4.2.min.js"></script>
        <script type="text/javascript"
        src="js/jquery/jquery-ui-1.7.2.custom/js/jquery-ui-1.7.2.custom.min.js"></script>
        <script type="text/javascript"
        src="js/jquery/fullcalendar-1.4.5/fullcalendar.min.js"></script>
        <script type="text/javascript"
        src="js/default.js"></script>
        <script type="text/javascript">
            var isAdmin = <?php echo ($isAdmin) ? "true" : "false"; ?>;
            $(document).ready(function(){
                
                $(window).resize(function () {
                    alertSize();
                });

                $("#homeTabsContainer").tabs({
                    select: function(event, ui) { 
                        if (ui.index == 1 && !isAdmin) {
                            populateEditMyUserAccountForm();
                        } else if (ui.index == 4 && isAdmin) {
                            populateEditMyUserAccountForm();
                        }
                        if (ui.index == 1 && isAdmin) {
                            populateAddResourceOwnerInputField();
                            populateEditResourceOwnerInputField();
                        }
                        if (ui.index == 2 && isAdmin) {
                            loadReservationResourcesListContainer();
                            $("#fullcalendar").fullCalendar("refetchEvents");
                        } else if (ui.index == 0 && !isAdmin) {
                            loadReservationResourcesListContainer();
                        }
                    },
                    show: function(event, ui) {
                        $('#fullcalendar').fullCalendar('render');
                        if (ui.index == 0 && !isAdmin) {
                            loadReservationResourcesListContainer();
                        }
                    }
                });

                loadInputFieldsEffects();
                
                $(function() {
                    
                    $("#addResourceReservationFormDialogContainer").dialog({
                        autoOpen: false,
                        height: 350,
                        width: 350,
                        modal: true,
                        close: function(event, ui) {
                            $("#addResourceReservationForm")[0].reset();
                            $("#addResourceReservationReoccurrenceId").val(0);
                            $("#addResourceReservationStopDate").val("");
                            $("#addResourceReservationStopDate").attr("disabled", true);
                            $("#addResourceReservationEndDate").removeClass("errorField");
                            $("#addResourceReservationEndDateLabel").removeClass("errorLabel");
                            $("#addResourceReservationStopDate").removeClass("errorField");
                            $("#addResourceReservationStopDateLabel").removeClass("errorLabel");
                        }
                    });

                    $("#deleteResourceReservationFormDialogContainer").dialog({
                        autoOpen: false,
                        height: 350,
                        width: 350,
                        modal: true,
                        close: function(event, ui) {
                            
                        }
                    });
                });
                
                $("#fullcalendar").fullCalendar({
                    events: "ajaxLoadReservationCalendarData.php",
                    dayClick: function(date, allDay, jsEvent, view) {

                        if ($("#addResourceReservationId").val() == 0) {
                            alert("Please select a resource.");
                            return false;
                        }

                        $('#addResourceReservationFormDialogContainer').dialog('open');
                        $('#addResourceReservationStartDate').val($.fullCalendar.formatDate(date, "MM/dd/yyyy"));

                        $("#addResourceReservationEndDate").datepicker("option", "minDate", new Date($.fullCalendar.formatDate(date, "MM/dd/yyyy")));
                        $("#addResourceReservationEndDate").datepicker( "option", "defaultDate", new Date($.fullCalendar.formatDate(date, "MM/dd/yyyy")));

                        $("#addResourceReservationStopDate").datepicker("option", "minDate", new Date($.fullCalendar.formatDate(date, "MM/dd/yyyy")));
                        $("#addResourceReservationStopDate").datepicker( "option", "defaultDate", new Date($.fullCalendar.formatDate(date, "MM/dd/yyyy")));
                    },
                    eventClick: function(calEvent, jsEvent, view) {
                        //alert('Event: ' + calEvent.id);
                        //alert('Coordinates: ' + jsEvent.pageX + ',' + jsEvent.pageY);
                        //alert('View: ' + view.name);
                        // change the border color just for fun
                        //$(this).css('border-color', 'red');
                        if (calEvent.className != "disableReservation") {
                            $("#reservationIdToBeDeleted").val(calEvent.id);
                            $('#deleteResourceReservationFormDialogContainer').dialog('open');
                        }
                    }
                });

                alertSize();

                $("#addResourceReservationEndDate").datepicker();
                $("#addResourceReservationStopDate").datepicker();

            });
        </script>
    </head>
    <body>
        <div id="main">
            <div id="topStatusBar">
                <?php include "includes/topStatusBarInclude.php"; ?>
            </div>
            <div id="homeLogoContainer">
                <div style="float: left;">
                    <img src="images/logo.png"
                         alt="logo"/>
                </div>
                <div style="float: right;margin-right: 10px;background-color: #333333;padding: 10px;color: #FFFFFF;">
                    Hello, <span id="authenticatedUsernameContainer"><?php echo $_SESSION["username"]; ?></span> |
                    <a href="index.php">Logout</a>
                </div>
            </div>
            <div  id="homeContentContainer">
                <?php
                if ($isAdmin) {
                    ?>
                <div id="homeTabsContainer">
                    <ul>
                        <li><a href="#tabs-1">Manage Users Accounts</a></li>
                        <li><a href="#tabs-2">Manage Resources</a></li>
                        <li><a href="#tabs-3">Manage Reservations</a></li>
                        <li><a href="#tabs-4">Reports</a></li>
                        <li><a href="#tabs-5">My Account</a></li>
                    </ul>
                    <div id="tabs-1">
                            <?php include "includes/manageUsersAccountsTabInclude.php"; ?>
                    </div>
                    <div id="tabs-2">
                            <?php include "includes/manageResourcesTabInclude.php"; ?>
                    </div>
                    <div id="tabs-3">
                            <?php include "includes/manageReservationsHomeTabInclude.php"; ?>
                    </div>
                    <div id="tabs-4">
                        <?php include "includes/reportsTabInclude.php"; ?>
                    </div>
                    <div id="tabs-5">
                            <?php include "includes/myAccountTabInclude.php"; ?>
                    </div>
                </div>
                    <?php
                } else {
                    ?>
                <div id="homeTabsContainer">
                    <ul>
                        <li><a href="#tabs-1">Manage Reservations</a></li>
                        <li><a href="#tabs-2">My Account</a></li>
                    </ul>
                    <div id="tabs-1">
                        <?php include "includes/manageReservationsHomeTabInclude.php"; ?>
                    </div>
                    <div id="tabs-2">
                            <?php include "includes/myAccountTabInclude.php"; ?>
                    </div>
                </div>
                    <?php
                }
                ?>
            </div>
        </div>
    </body>
</html>
